﻿using System;
using System.Collections.Generic;
using System.Text;
namespace Odyssey.Consul.Features
{
    public class QueryParameter
    {
        public static readonly QueryParameter Default = new QueryParameter();

        public Dictionary<string, string> OtherParameters { get; }

        public QueryParameter()
        {
            OtherParameters = new Dictionary<string, string>();
            Datacenter = string.Empty;
            WaitIndex = 0;
        }

        public string Datacenter { get; set; }

        public int WaitIndex { get; set; }

        public TimeSpan? WaitTime { get; set; }

        private string OtherParametersToString(string stringHeader = "?")
        {
            var otherParametersStringBuilder = new StringBuilder(stringHeader);
            foreach (var item in OtherParameters)
            {
                otherParametersStringBuilder.Append($"{item.Key}={item.Value}&");
            }
            return otherParametersStringBuilder.ToString().TrimEnd('&');
        }


        private string TimeSpanToString(TimeSpan timeSpan)
        {
            if (timeSpan == TimeSpan.Zero)
            {
                return "0";
            }

            if (timeSpan.TotalSeconds < 1)
            {
                return timeSpan.TotalMilliseconds.ToString("#ms");
            }
            else
            {
                var timeSpanStringBuilder = new StringBuilder();
                if ((int)timeSpan.TotalHours > 0)
                {
                    timeSpanStringBuilder.Append(timeSpan.TotalHours.ToString("#h"));
                }
                if (timeSpan.Minutes > 0)
                {
                    timeSpanStringBuilder.Append(timeSpan.ToString("%m'm'"));
                }
                if (timeSpan.Seconds > 0)
                {
                    timeSpanStringBuilder.Append(timeSpan.ToString("%s"));
                }
                if (timeSpan.Milliseconds > 0)
                {
                    timeSpanStringBuilder.Append(".");
                    timeSpanStringBuilder.Append(timeSpan.ToString("fff"));
                }
                if (timeSpan.Seconds > 0)
                {
                    timeSpanStringBuilder.Append("s");
                }
                return timeSpanStringBuilder.ToString();
            }
        }

        public override string ToString()
        {
            if (this.Equals(QueryParameter.Default))
            {
                return string.Empty;
            }
            //var stringBuilder = new StringBuilder();
            //stringBuilder.Append("?");
            if (!string.IsNullOrWhiteSpace(Datacenter))
            {
                OtherParameters.TryAdd("dc", Datacenter);
            }
            if (WaitIndex > 0)
            {
                OtherParameters.TryAdd("index", WaitIndex.ToString());
            }
            if (WaitTime.HasValue)
            {
                OtherParameters.TryAdd("wait", TimeSpanToString((TimeSpan)WaitTime));
            }
            if (OtherParameters.Count == 0) {
                return string.Empty;    
            }
            return OtherParametersToString();
        }
    }
}
